package com.sailfish.springbootdemo.dao.db7;

import com.sailfish.springbootdemo.common.DB7TableName;
import org.apache.ibatis.annotations.*;
import com.sailfish.springbootdemo.pojo.db7.DynamicVplData;

import java.util.List;

public interface TbDynamicVplDataDao {
        @Insert("<script>" +
                "INSERT INTO " + DB7TableName.TB_DYNAMIC_VPL_DATA +
                " <trim prefix='(' suffix=')' suffixOverrides=','>" +
                "   meta_id, row_index, column_id," +
                "   <if test='cellValue != null'>cell_value,</if>" +
                "   create_time, update_time" +
                " </trim>" +
                " VALUES " +
                " <foreach item='item' collection='list' separator=','>" +
                "   <trim prefix='(' suffix=')' suffixOverrides=','>" +
                "     #{item.metaId}, #{item.rowIndex}, #{item.columnId}," +
                "     <if test='item.cellValue != null'>#{item.cellValue},</if>" +
                "     NOW(), NOW()" +
                "   </trim>" +
                " </foreach>" +
                "</script>")
        int batchInsert(List<DynamicVplData> dataList);

        @Update("<script>" +
                "UPDATE " + DB7TableName.TB_DYNAMIC_VPL_DATA +
                " <set>" +
                "   <if test='cellValue != null'>cell_value = #{cellValue},</if>" +
                "   update_time = NOW()" +
                " </set>" +
                " WHERE id = #{id}" +
                "</script>")
        int update(DynamicVplData data);

        @Select("SELECT * FROM " + DB7TableName.TB_DYNAMIC_VPL_DATA + " WHERE meta_id = #{metaId} AND row_index = #{rowIndex}")
        List<DynamicVplData> selectByRow(@Param("metaId") Integer metaId,
                                       @Param("rowIndex") Integer rowIndex);

        @Delete("DELETE FROM " + DB7TableName.TB_DYNAMIC_VPL_DATA + "  WHERE meta_id = #{metaId}")
        int deleteByMetaId(Integer metaId);

        @Select("SELECT MAX(row_index) FROM " + DB7TableName.TB_DYNAMIC_VPL_DATA + " WHERE meta_id = #{metaId}")
        int getMaxRowIndex(@Param("metaId") Integer metaId);
}
